<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-rc.20" />
    <style>
      :root {
        --vp-c-bg: #fff;
      }

      [data-theme='dark'] {
        --vp-c-bg: #1b1b1f;
      }

      html,
      body {
        background-color: var(--vp-c-bg);
      }
    </style>
    <script>
      const useChoice = localStorage.getItem('vuepress-color-scheme')
      const systemStatus =
        'matchMedia' in window
          ? window.matchMedia('(prefers-color-scheme: dark)').matches
          : false

      if (useChoice === 'light') {
        document.documentElement.dataset.theme = 'light'
      } else if (useChoice === 'dark' || systemStatus) {
        document.documentElement.dataset.theme = 'dark'
      }
    </script>
    <link rel="stylesheet" href="/styles/index.scss"><title>哈佛大学Nature的这套生信分析代码，值得学 | Mengfei Li</title><meta name="description" content="用AI识别细胞并计数">
    <link rel="preload" href="/assets/style-DmsBt1IC.css" as="style"><link rel="stylesheet" href="/assets/style-DmsBt1IC.css">
    <link rel="modulepreload" href="/assets/app-9eKqhTlB.js"><link rel="modulepreload" href="/assets/article-2.html-CiPVO5cN.js">
    <link rel="prefetch" href="/assets/cv.html-IPIW7Oru.js" as="script"><link rel="prefetch" href="/assets/index.html-xHyYeCz8.js" as="script"><link rel="prefetch" href="/assets/article-1.html-Bkf059wN.js" as="script"><link rel="prefetch" href="/assets/article-1.html-BXVOm6K8.js" as="script"><link rel="prefetch" href="/assets/article-2.html-BuWg0PQK.js" as="script"><link rel="prefetch" href="/assets/article-3.html-Bb5X-Lb4.js" as="script"><link rel="prefetch" href="/assets/article-1.html-CBRe6dB9.js" as="script"><link rel="prefetch" href="/assets/article-2.html-BDd86Uxg.js" as="script"><link rel="prefetch" href="/assets/article-3.html-enrAF_HM.js" as="script"><link rel="prefetch" href="/assets/article-4.html-DPzVtHsP.js" as="script"><link rel="prefetch" href="/assets/article-5.html-Cosb7Byj.js" as="script"><link rel="prefetch" href="/assets/article-6.html-BNOyfs2c.js" as="script"><link rel="prefetch" href="/assets/cv.html-B6rpJC0I.js" as="script"><link rel="prefetch" href="/assets/index.html-BIEjuEbH.js" as="script"><link rel="prefetch" href="/assets/article-1.html-pzP0UGyB.js" as="script"><link rel="prefetch" href="/assets/article-2.html-CHIMVeqq.js" as="script"><link rel="prefetch" href="/assets/article-1.html-BM-SEJLt.js" as="script"><link rel="prefetch" href="/assets/article-2.html-CRl3Iyt5.js" as="script"><link rel="prefetch" href="/assets/article-3.html-D62a5JUh.js" as="script"><link rel="prefetch" href="/assets/article-4.html-DKvA5jUP.js" as="script"><link rel="prefetch" href="/assets/article-5.html-Cpzl3I-J.js" as="script"><link rel="prefetch" href="/assets/article-6.html-Dpk732IB.js" as="script"><link rel="prefetch" href="/assets/article-1.html-Cdqx9Ooz.js" as="script"><link rel="prefetch" href="/assets/article-2.html-CgTLON_V.js" as="script"><link rel="prefetch" href="/assets/article-3.html-DdsbEG2h.js" as="script"><link rel="prefetch" href="/assets/404.html-Cuxwrm8u.js" as="script"><link rel="prefetch" href="/assets/index.html-rkc5j0xA.js" as="script"><link rel="prefetch" href="/assets/index.html-BAwRtri-.js" as="script"><link rel="prefetch" href="/assets/index.html-Dzo-oQPd.js" as="script"><link rel="prefetch" href="/assets/index.html-DeG5W2Hk.js" as="script"><link rel="prefetch" href="/assets/index.html-Ndo2cAaJ.js" as="script"><link rel="prefetch" href="/assets/index.html-CgeiMdFn.js" as="script"><link rel="prefetch" href="/assets/setupDevtools-QXEOFQJV-DSJAWLsw.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><div class="vp-theme-container external-link-icon" vp-container data-v-53848562><!--[--><header class="vp-navbar" vp-navbar><div class="vp-toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a class="route-link" href="/"><img class="vp-site-logo" src="https://vuejs.press/images/hero.png" alt="Mengfei Li"><span class="vp-site-name vp-hide-mobile" aria-hidden="true">Mengfei Li</span></a></span><div class="vp-navbar-items-wrapper" style=""><!--[--><!--]--><nav class="vp-navbar-items vp-hide-mobile" aria-label="site navigation"><!--[--><div class="vp-navbar-item"><a class="route-link auto-link" href="/" aria-label="Home"><!--[--><!--[--><!--]--><!--]-->Home<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/researchList/" aria-label="Research"><!--[--><!--[--><!--]--><!--]-->Research<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/repositoryList/" aria-label="Repositories"><!--[--><!--[--><!--]--><!--]-->Repositories<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/blogList/" aria-label="Blog"><!--[--><!--[--><!--]--><!--]-->Blog<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/cv.html" aria-label="CV"><!--[--><!--[--><!--]--><!--]-->CV<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><div class="vp-navbar-dropdown-wrapper"><button class="vp-navbar-dropdown-title" type="button" aria-label="Language"><span class="title">Language</span><span class="arrow down"></span></button><button class="vp-navbar-dropdown-title-mobile" type="button" aria-label="Language"><span class="title">Language</span><span class="right arrow"></span></button><ul style="display:none;" class="vp-navbar-dropdown"><!--[--><li class="vp-navbar-dropdown-item"><a class="route-link route-link-active auto-link" href="/blog/article-2.html" aria-label="English"><!--[--><!--[--><!--]--><!--]-->English<!--[--><!--[--><!--]--><!--]--></a></li><li class="vp-navbar-dropdown-item"><a class="route-link auto-link" href="/zh/blog/article-2.html" aria-label="中文"><!--[--><!--[--><!--]--><!--]-->中文<!--[--><!--[--><!--]--><!--]--></a></li><!--]--></ul></div></div><!--]--></nav><!--[--><!--]--><button type="button" class="vp-toggle-color-mode-button" title="toggle color mode"><svg class="light-icon" viewbox="0 0 32 32" style=""><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg class="dark-icon" viewbox="0 0 32 32" style="display:none;"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><!----></div></header><!--]--><div class="vp-sidebar-mask"></div><!--[--><aside class="vp-sidebar" vp-sidebar><nav class="vp-navbar-items" aria-label="site navigation"><!--[--><div class="vp-navbar-item"><a class="route-link auto-link" href="/" aria-label="Home"><!--[--><!--[--><!--]--><!--]-->Home<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/researchList/" aria-label="Research"><!--[--><!--[--><!--]--><!--]-->Research<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/repositoryList/" aria-label="Repositories"><!--[--><!--[--><!--]--><!--]-->Repositories<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/blogList/" aria-label="Blog"><!--[--><!--[--><!--]--><!--]-->Blog<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><a class="route-link auto-link" href="/cv.html" aria-label="CV"><!--[--><!--[--><!--]--><!--]-->CV<!--[--><!--[--><!--]--><!--]--></a></div><div class="vp-navbar-item"><div class="vp-navbar-dropdown-wrapper"><button class="vp-navbar-dropdown-title" type="button" aria-label="Language"><span class="title">Language</span><span class="arrow down"></span></button><button class="vp-navbar-dropdown-title-mobile" type="button" aria-label="Language"><span class="title">Language</span><span class="right arrow"></span></button><ul style="display:none;" class="vp-navbar-dropdown"><!--[--><li class="vp-navbar-dropdown-item"><a class="route-link route-link-active auto-link" href="/blog/article-2.html" aria-label="English"><!--[--><!--[--><!--]--><!--]-->English<!--[--><!--[--><!--]--><!--]--></a></li><li class="vp-navbar-dropdown-item"><a class="route-link auto-link" href="/zh/blog/article-2.html" aria-label="中文"><!--[--><!--[--><!--]--><!--]-->中文<!--[--><!--[--><!--]--><!--]--></a></li><!--]--></ul></div></div><!--]--></nav><!--[--><!--]--><ul class="vp-sidebar-items"><!--[--><li><p tabindex="0" class="vp-sidebar-item vp-sidebar-heading">哈佛大学Nature的这套生信分析代码，值得学 <!----></p><!----></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><div class="vp-page blog-page-wrapper" data-v-53848562><aside class="vp-blog-info-wrapper" style="transition:transform 0.25s ease-in-out 0.16s, opacity 0.25s ease-in-out 0.16s;transform:translateY(0px);opacity:1;" data-v-53848562><div class="vp-blogger-info" vocab="https://schema.org/" typeof="Person"><div class="vp-blogger" data-balloon-pos="down"><img class="vp-blogger-avatar" src="/images/profile.png" property="image" alt="Blogger Avatar" loading="lazy"><div class="vp-blogger-name" property="name">Mengfei Li</div><div class="vp-blogger-description">biological science</div><div class="vp-blogger-description">Undergraduate</div></div><div class="vp-social-medias"><div class="vp-social-media-box"><a class="vp-social-media-a" href="mailto:2935580139@qq.com" rel="noopener noreferrer" target="_blank" aria-label="Email" data-balloon-pos="up"><svg xmlns="http://www.w3.org/2000/svg" class="vp-social-media-icon email-icon" viewBox="0 0 1024 1024"><circle cx="512" cy="512" r="512" fill="#1384FF"></circle><path fill="#fff" d="M270.077 286.233H751.99c32.933 0 59.86 24.855 60.274 55.51l-301.023 157L210.217 341.88c.207-30.723 26.927-55.717 59.86-55.717zm-59.929 115.714-.276 277.756c0 30.931 27.134 56.2 60.205 56.2H751.99c33.14 0 60.274-25.269 60.274-56.2V401.81L518.283 551.492a15.88 15.88 0 0 1-14.43 0L210.148 401.947z"></path></svg><span class="vp-social-media-text">Email</span></a><a class="vp-social-media-a" href="https://github.com/lmf04/" rel="noopener noreferrer" target="_blank" aria-label="GitHub" data-balloon-pos="up"><svg xmlns="http://www.w3.org/2000/svg" class="vp-social-media-icon github-icon" viewBox="0 0 1024 1024"><circle cx="512" cy="512" r="512" fill="#171515"></circle><path fill="#fff" d="M509.423 146.442c-200.317 0-362.756 162.42-362.756 362.8 0 160.266 103.936 296.24 248.109 344.217 18.139 3.327 24.76-7.872 24.76-17.486 0-8.613-.313-31.427-.49-61.702-100.912 21.923-122.205-48.63-122.205-48.63-16.495-41.91-40.28-53.067-40.28-53.067-32.937-22.51 2.492-22.053 2.492-22.053 36.407 2.566 55.568 37.386 55.568 37.386 32.362 55.438 84.907 39.43 105.58 30.143 3.296-23.444 12.667-39.43 23.032-48.498-80.557-9.156-165.246-40.28-165.246-179.297 0-39.604 14.135-71.988 37.342-97.348-3.731-9.178-16.18-46.063 3.556-96.009 0 0 30.46-9.754 99.76 37.19 28.937-8.048 59.97-12.071 90.823-12.211 30.807.14 61.843 4.165 90.822 12.21 69.26-46.944 99.663-37.189 99.663-37.189 19.792 49.946 7.34 86.831 3.61 96.01 23.25 25.359 37.29 57.742 37.29 97.347 0 139.366-84.82 170.033-165.637 179.013 13.026 11.2 24.628 33.342 24.628 67.182 0 48.498-.445 87.627-.445 99.521 0 9.702 6.535 20.988 24.945 17.444 144.03-48.067 247.881-183.95 247.881-344.175 0-200.378-162.442-362.798-362.802-362.798z"></path></svg><span class="vp-social-media-text">github</span></a><a class="vp-social-media-a" href="https://lmf04.github.io/" rel="noopener noreferrer" target="_blank" aria-label="Gitlab" data-balloon-pos="up"><svg xmlns="http://www.w3.org/2000/svg" class="vp-social-media-icon gitlab-icon" viewBox="0 0 1024 1024"><circle cx="512" cy="512" r="512" fill="#E8F0FF"></circle><path fill="#E24329" d="m512 808 110-340H402z"></path><path fill="#FC6D26" d="M512 808 402 468H247z"></path><path fill="#E24329" d="M247 468h155l-67-204c-4-11-19-11-22 0z"></path><path fill="#FC6D26" d="m512 808 110-340h156z"></path><path fill="#FCA326" d="m778 468 33 104c3 10-1 19-9 25L512 808z"></path><path fill="#E24329" d="M778 468H622l68-204c3-11 18-11 21 0z"></path><path fill="#FCA326" d="M512 808 222 597c-8-5-12-16-9-25l34-104z"></path></svg><span class="vp-social-media-text">Website</span></a><a class="vp-social-media-a" href="https://www.sdu.edu.cn/" rel="noopener noreferrer" target="_blank" aria-label="Gitlab" data-balloon-pos="up"><svg t="1741831836813" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9703" width="32" height="32"><path d="M512 976.213333A464.213333 464.213333 0 1 0 512 47.786667a464.213333 464.213333 0 0 0 0 928.426666z m0 47.786667a512 512 0 1 1 0-1024 512 512 0 0 1 0 1024z" fill="#009688" p-id="9704"></path><path d="M88.746667 513.365333a424.618667 424.618667 0 1 1 849.373866 0 424.618667 424.618667 0 0 1-849.373866 0z m414.72-14.336c-120.695467 0-179.882667 77.550933-179.882667 77.550934V737.28s61.576533 60.347733 189.371733 60.347733c127.7952 0 170.3936-60.347733 170.3936-60.347733V576.580267S624.162133 499.029333 503.466667 499.029333z m2.389333 264.192c-83.626667 0-151.415467-20.48-151.415467-45.943466 0-25.258667 67.7888-45.943467 151.415467-45.943467s151.552 20.48 151.552 45.943467c-0.068267 25.326933-67.925333 46.011733-151.552 46.011733zM111.8208 464.622933L291.566933 552.277333s87.04-107.656533 207.121067-107.656533c117.282133 0 233.198933 107.656533 233.198933 107.656533l65.058134-33.041066v96.733866c-10.376533 4.232533-17.749333 15.701333-17.749334 29.4912 0 14.062933 7.645867 25.6 18.090667 29.696l-19.182933 65.1264h56.7296l-19.456-65.604266c9.352533-4.778667 15.837867-15.9744 15.837866-29.0816a32.290133 32.290133 0 0 0-15.36-28.672v-107.178667l94.685867-47.854933-416.631467-226.781867-382.1568 229.512533z" fill="#009688" p-id="9705"></path></svg><span class="vp-social-media-text">Shandong University</span></a></div></div></div></aside><div class="vp-blog-main" data-v-53848562><div class="vp-blog-main-box" data-v-53848562><div data-v-53848562><h1 data-v-53848562>哈佛大学Nature的这套生信分析代码，值得学</h1><p data-v-53848562>2025-02-12</p><div data-v-53848562><p>近日，哈佛大学的研究团队在《自然》杂志上发表了一项重要研究成果，该研究深入剖析了哺乳动物下丘脑视前区（POA）神经元的发育轨迹，揭示了感觉输入、性别和功能等因素对其发育的显著影响，为我们理解本能行为的神经基础提供了全新的视角。 Image Image Image Image Image Image Image Image Image Image Image Image 学习下下面这个图还有 fig3 Image Image</p><h1 id="此代码块用于设置环境-加载后续分析所需的r包" tabindex="-1"><a class="header-anchor" href="#此代码块用于设置环境-加载后续分析所需的r包"><span>此代码块用于设置环境，加载后续分析所需的R包</span></a></h1><div class="language-text line-numbers-mode" data-highlighter="prismjs" data-ext="text"><pre><code><span class="line"># 此代码用于生成区域化相关矩阵和指标，对应扩展图3d - k</span>
<span class="line"># 以兴奋性神经元为例进行分析</span>
<span class="line"># 加载必要的库</span>
<span class="line">library(Seurat)  # 用于单细胞RNA测序数据分析的强大工具包</span>
<span class="line">library(ggplot2) # 用于创建高质量的图形</span>
<span class="line">library(patchwork) # 用于组合多个ggplot图形</span>
<span class="line">library(magrittr) # 提供管道操作符 %&gt;%</span>
<span class="line">library(tidyverse) # 包含多个数据处理和可视化的常用包</span>
<span class="line">library(pheatmap) # 用于绘制热图</span>
<span class="line">library(palr) # 提供颜色调色板</span>
<span class="line">此代码块用于生成给定年龄下细胞类型之间的相关矩阵</span>
<span class="line"># 此部分生成如扩展图3d、e、g、h所示的矩阵，以图e（P65年龄的兴奋性神经元）为例</span>
<span class="line"># 加载合并后的数据</span>
<span class="line"># 合并后的数据仅包含兴奋性神经元（如果分析抑制性神经元，操作相同，只是需分别运行），</span>
<span class="line"># 这些数据是跨所有年龄合并的，然后使用SCTransform在整个数据集中进行归一化处理</span>
<span class="line">excit.merge &lt;- readRDS(&quot;&quot;)</span>
<span class="line"># 从合并数据中提取P65年龄的数据</span>
<span class="line">p65 &lt;- subset(excit.merge, subset = age == &quot;p65&quot;)</span>
<span class="line"># 对细胞类型名称进行排序</span>
<span class="line">celltypes &lt;- names(table(p65$my.cell.type))</span>
<span class="line">celltypes &lt;- c(celltypes[1:19],celltypes[21:28],celltypes[20],celltypes[29:37],celltypes[39:46],celltypes[38],celltypes[47],celltypes[50:57],celltypes[48:49],celltypes[58:64])</span>
<span class="line"># 重新排序细胞类型</span>
<span class="line">new.order &lt;- c(grep(&quot;B&quot;,celltypes),grep(&quot;H&quot;,celltypes),grep(&quot;L&quot;,celltypes),grep(&quot;C&quot;,celltypes),grep(&quot;F&quot;,celltypes),grep(&quot;M&quot;,celltypes),grep(&quot;N&quot;,celltypes),grep(&quot;A&quot;,celltypes),grep(&quot;P&quot;,celltypes),grep(&quot;T&quot;,celltypes),grep(&quot;X&quot;,celltypes))</span>
<span class="line">celltypes &lt;- celltypes[new.order]</span>
<span class="line"># 设置参数：特征基因数量</span>
<span class="line">nfeatures = 20000</span>
<span class="line"># 设置默认分析的数据集为RNA</span>
<span class="line">DefaultAssay(p65)&lt;-&quot;RNA&quot;</span>
<span class="line"># 找出可变特征基因</span>
<span class="line">p65 &lt;- FindVariableFeatures(p65,nfeatures=nfeatures)</span>
<span class="line"># 对数据进行缩放</span>
<span class="line">p65 &lt;- ScaleData(p65)</span>
<span class="line"># 生成质心矩阵，该矩阵将作为corr()函数的输入</span>
<span class="line">centroid.mat&lt;-matrix(,nrow = length(celltypes),ncol = nfeatures)</span>
<span class="line">for (i in 1:length(celltypes)){</span>
<span class="line">  # 每处理10个细胞类型打印一次进度信息</span>
<span class="line">  if (i %% 10 == 0){</span>
<span class="line">    print(paste0(&quot;on cell type number &quot;,i))</span>
<span class="line">  }</span>
<span class="line">  # 如果某个细胞类型的细胞数量少于2个，则跳过该细胞类型</span>
<span class="line">  if (table(p65$my.cell.type)[i] &lt; 2){</span>
<span class="line">    print(&quot;NOTE: this cell type has less than 2 cells. skipping...&quot;)</span>
<span class="line">    next</span>
<span class="line">  }</span>
<span class="line">  # 提取当前细胞类型的数据</span>
<span class="line">  ct.sub &lt;- subset(p65,subset = my.cell.type == celltypes[i])</span>
<span class="line">  # 获取缩放后的数据矩阵</span>
<span class="line">  scaledata.mat &lt;- ct.sub@assays$RNA@scale.data</span>
<span class="line">  # 计算当前细胞类型的质心（即每行的均值）</span>
<span class="line">  centroid.mat[i,] &lt;- rowMeans(scaledata.mat)</span>
<span class="line">}</span>
<span class="line"># 计算质心矩阵的相关性矩阵</span>
<span class="line">p65.corrmat &lt;- cor(t(centroid.mat))</span>
<span class="line"># 设置相关性矩阵的行名和列名</span>
<span class="line">rownames(p65.corrmat) &lt;- celltypes</span>
<span class="line">colnames(p65.corrmat) &lt;- celltypes</span>
<span class="line"># 绘制热图，不进行行和列的聚类</span>
<span class="line">pheatmap(p65.corrmat,cluster_rows = F,cluster_cols = F,border_color = NA)</span>
<span class="line"># 对相关性矩阵进行缩放</span>
<span class="line">scale.max &lt;- 0.5</span>
<span class="line">scale.min &lt;- -0.2</span>
<span class="line">scale.corrmat &lt;- p65.corrmat</span>
<span class="line"># 将大于scale.max的值设置为scale.max</span>
<span class="line">scale.corrmat[scale.corrmat &gt; scale.max] &lt;- scale.max</span>
<span class="line"># 将小于scale.min的值设置为scale.min</span>
<span class="line">scale.corrmat[scale.corrmat &lt; scale.min] &lt;- scale.min</span>
<span class="line"># 绘制缩放后的热图</span>
<span class="line">pheatmap(scale.corrmat,cluster_rows = F,cluster_cols = F,border_color = NA)</span>
<span class="line">此代码块用于生成每个年龄下区域标记基因的表达情况</span>
<span class="line"># 此部分代码用于生成扩展图3j - k</span>
<span class="line"># 首先创建一个用于绘制热图的函数</span>
<span class="line">avg.gene.heatmap &lt;- function(obj,</span>
<span class="line">                             br.or.ct,  # &quot;br&quot; 表示按照脑区域进行操作，&quot;ct&quot; 表示按照细胞类型进行操作</span>
<span class="line">                             identity.rows,  # 按顺序排列的细胞类型或脑区域名称</span>
<span class="line">                             gene.columns,  # 按顺序排列的基因名称</span>
<span class="line">                             scale.max = 2,  # 热图数据缩放的最大值</span>
<span class="line">                             scale.min = 0,  # 热图数据缩放的最小值</span>
<span class="line">                             return.matrix = 0) {  # 是否返回热图矩阵，0 表示不返回，1 表示返回</span>
<span class="line">  </span>
<span class="line">  # 设置默认分析的数据集为 RNA</span>
<span class="line">  DefaultAssay(obj) &lt;- &quot;RNA&quot;</span>
<span class="line">  </span>
<span class="line">  # 将所有基因设置为可变特征基因，以便后续进行缩放</span>
<span class="line">  VariableFeatures(obj) &lt;- rownames(obj)</span>
<span class="line">  </span>
<span class="line">  # 对数据进行缩放处理</span>
<span class="line">  obj &lt;- ScaleData(obj)</span>
<span class="line">  </span>
<span class="line">  # 初始化一个空矩阵，用于存储热图的数据</span>
<span class="line">  heatmap.to.plot &lt;- matrix(, nrow = length(identity.rows), ncol = length(gene.columns))</span>
<span class="line">  </span>
<span class="line">  # 遍历每一行（细胞类型或脑区域）</span>
<span class="line">  for (ident.row in 1:length(identity.rows)) {</span>
<span class="line">    if (br.or.ct == &quot;br&quot;) {</span>
<span class="line">      # 如果是按照脑区域操作，提取对应脑区域的数据</span>
<span class="line">      ident.sub &lt;- subset(obj, subset = brain.region == identity.rows[ident.row])</span>
<span class="line">    }</span>
<span class="line">    if (br.or.ct == &quot;ct&quot;) {</span>
<span class="line">      # 如果是按照细胞类型操作，提取对应细胞类型的数据</span>
<span class="line">      ident.sub &lt;- subset(obj, subset = my.cell.type == identity.rows[ident.row])</span>
<span class="line">    }</span>
<span class="line">    </span>
<span class="line">    # 遍历每一列（基因）</span>
<span class="line">    for (gene in 1:length(gene.columns)) {</span>
<span class="line">      # 查找当前基因在数据中的索引</span>
<span class="line">      gene.ind &lt;- grep(paste0(&quot;^&quot;, gene.columns[gene], &quot;$&quot;), rownames(ident.sub))</span>
<span class="line">      </span>
<span class="line">      # 计算当前基因在当前细胞类型或脑区域中的平均表达值，并存储到热图矩阵中</span>
<span class="line">      heatmap.to.plot[ident.row, gene] &lt;- mean(ident.sub@assays$RNA@scale.data[gene.ind, ])</span>
<span class="line">    }</span>
<span class="line">  }</span>
<span class="line">  </span>
<span class="line">  # 设置热图矩阵的行名和列名</span>
<span class="line">  rownames(heatmap.to.plot) &lt;- identity.rows</span>
<span class="line">  colnames(heatmap.to.plot) &lt;- gene.columns</span>
<span class="line">  </span>
<span class="line">  # 对热图矩阵进行缩放处理</span>
<span class="line">  heatmap.to.plot.scaled &lt;- heatmap.to.plot</span>
<span class="line">  heatmap.to.plot.scaled[heatmap.to.plot.scaled &gt; scale.max] &lt;- scale.max</span>
<span class="line">  heatmap.to.plot.scaled[heatmap.to.plot.scaled &lt; scale.min] &lt;- scale.min</span>
<span class="line">  </span>
<span class="line">  # 绘制热图，不进行行和列的聚类，使用反转的颜色调色板</span>
<span class="line">  pheatmap(t(heatmap.to.plot.scaled), cluster_rows = F, cluster_cols = F, color = rev(bathy_deep_pal(50)), border_color = NA)</span>
<span class="line">  </span>
<span class="line">  # 如果 return.matrix 为 1，则返回热图矩阵</span>
<span class="line">  if (return.matrix == 1) {</span>
<span class="line">    return(heatmap.to.plot)</span>
<span class="line">  }</span>
<span class="line">}</span>
<span class="line"># 现在获取标记基因并绘制热图</span>
<span class="line"># 将合并数据中的脑区域转换为因子类型，并指定水平顺序</span>
<span class="line">excit.merge$brain.region &lt;- factor(excit.merge$brain.region, levels = c(&quot;BNST&quot;, &quot;HDB.VLPO&quot;, &quot;LPO.PS&quot;, &quot;BAC-like&quot;, &quot;PeFA&quot;, &quot;MPN.anterior&quot;, &quot;MPN.posterior&quot;, &quot;AvPE.MnPO&quot;, &quot;PVN&quot;, &quot;PVT&quot;, &quot;Mixed/Unknown&quot;))</span>
<span class="line"># 设置标识为脑区域，以便后续分析</span>
<span class="line">Idents(excit.merge) &lt;- &quot;brain.region&quot;</span>
<span class="line"># 数据准备</span>
<span class="line"># 提取 P65 年龄的数据</span>
<span class="line">excit.p65 &lt;- subset(excit.merge, subset = age == &quot;p65&quot;)</span>
<span class="line"># 提取 E16 年龄的数据</span>
<span class="line">excit.e16 &lt;- subset(excit.merge, subset = age == &quot;e16&quot;)</span>
<span class="line"># 获取所有脑区域的名称</span>
<span class="line">all.br &lt;- names(table(excit.p65$brain.region))</span>
<span class="line"># 获取 P65 年龄的区域标记基因</span>
<span class="line">region.markers.p65 &lt;- FindAllMarkers(excit.p65, assay = &quot;RNA&quot;, only.pos = T)</span>
<span class="line"># 筛选出调整后 P 值小于 0.05 的标记基因</span>
<span class="line">region.markers.p65 &lt;- region.markers.p65[region.markers.p65$p_val_adj &lt; 0.05, ]</span>
<span class="line"># 筛选出平均对数 2 倍变化大于 0.5 的标记基因</span>
<span class="line">region.markers.p65 &lt;- region.markers.p65[region.markers.p65$avg_log2FC &gt; 0.5, ]</span>
<span class="line"># 去除重复的标记基因</span>
<span class="line">region.markers.p65 &lt;- region.markers.p65$gene[-which(duplicated(region.markers.p65$gene))]</span>
<span class="line"># 以热图形式展示 P65 年龄的标记基因在 P65 时的表达情况</span>
<span class="line">avg.gene.heatmap(excit.p65, &quot;br&quot;, all.br, region.markers.p65)</span>
<span class="line"># 现在测试 P65 年龄的基因在 E16 时的表达情况</span>
<span class="line">avg.gene.heatmap(excit.e16, &quot;br&quot;, all.br, region.markers.p65)</span>
<span class="line"># 反之，获取 E16 年龄的区域标记基因</span>
<span class="line">region.markers.e16 &lt;- FindAllMarkers(excit.e16, assay = &quot;RNA&quot;, only.pos = T)</span>
<span class="line"># 筛选出调整后 P 值小于 0.05 的标记基因</span>
<span class="line">region.markers.e16 &lt;- region.markers.e16[region.markers.e16$p_val_adj &lt; 0.05, ]</span>
<span class="line"># 筛选出平均对数 2 倍变化大于 0.5 的标记基因</span>
<span class="line">region.markers.e16 &lt;- region.markers.e16[region.markers.e16$avg_log2FC &gt; 0.5, ]</span>
<span class="line"># 去除重复的标记基因</span>
<span class="line">region.markers.e16 &lt;- region.markers.e16$gene[-which(duplicated(region.markers.e16$gene))]</span>
<span class="line"># 以热图形式展示 E16 年龄的标记基因在 E16 时的表达情况</span>
<span class="line">avg.gene.heatmap(excit.e16, &quot;br&quot;, all.br, region.markers.e16)</span>
<span class="line"># 现在测试 E16 年龄的基因在 P65 时的表达情况</span>
<span class="line">avg.gene.heatmap(excit.p65, &quot;br&quot;, all.br, region.markers.e16)</span>
<span class="line"># 现在去除同时出现在两个列表中的标记基因，并重新绘制热图</span>
<span class="line"># 获取仅在 P65 中出现的标记基因</span>
<span class="line">unique.p65.markers &lt;- setdiff(region.markers.p65, region.markers.e16)</span>
<span class="line"># 获取仅在 E16 中出现的标记基因</span>
<span class="line">unique.e16.markers &lt;- setdiff(region.markers.e16, region.markers.p65)</span>
<span class="line"># 绘制相关热图</span>
<span class="line">avg.gene.heatmap(excit.p65, &quot;br&quot;, all.br, unique.p65.markers)</span>
<span class="line">avg.gene.heatmap(excit.e16, &quot;br&quot;, all.br, unique.p65.markers)</span>
<span class="line">avg.gene.heatmap(excit.e16, &quot;br&quot;, all.br, unique.e16.markers)</span>
<span class="line">avg.gene.heatmap(excit.p65, &quot;br&quot;, all.br, unique.e16.markers)</span>
<span class="line"># 现在，探讨：标记基因表达模式在什么年龄开始接近成年？</span>
<span class="line"># 简单地通过矩阵相减来分析</span>
<span class="line"># 提取不同年龄的数据</span>
<span class="line">excit.p28 &lt;- subset(excit.merge.noNA, subset = age == &quot;p28&quot;)</span>
<span class="line">excit.p18 &lt;- subset(excit.merge.noNA, subset = age == &quot;p18&quot;)</span>
<span class="line">excit.p10 &lt;- subset(excit.merge.noNA, subset = age == &quot;p10&quot;)</span>
<span class="line">excit.p4 &lt;- subset(excit.merge.noNA, subset = age == &quot;p4&quot;)</span>
<span class="line">excit.p0 &lt;- subset(excit.merge.noNA, subset = age == &quot;p0&quot;)</span>
<span class="line">excit.e18 &lt;- subset(excit.merge.noNA, subset = age == &quot;e18&quot;)</span>
<span class="line"># 对于 P65 年龄的标记基因（不包括 E16 的标记基因）</span>
<span class="line"># 获取不同年龄下这些标记基因的表达矩阵</span>
<span class="line">p65.mat &lt;- avg.gene.heatmap(excit.p65, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">p28.mat &lt;- avg.gene.heatmap(excit.p28, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">p18.mat &lt;- avg.gene.heatmap(excit.p18, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">p10.mat &lt;- avg.gene.heatmap(excit.p10, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">p4.mat &lt;- avg.gene.heatmap(excit.p4, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">p0.mat &lt;- avg.gene.heatmap(excit.p0, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">e18.mat &lt;- avg.gene.heatmap(excit.e18, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line">e16.mat &lt;- avg.gene.heatmap(excit.e16, &quot;br&quot;, all.br, unique.p65.markers, return = 1)</span>
<span class="line"># 计算不同年龄下的表达矩阵与 P65 年龄的表达矩阵的相关性</span>
<span class="line">cor.to.p65 &lt;- data.frame(age = 1:8, cor = c(cor(c(e16.mat), c(p65.mat)),</span>
<span class="line">                                             cor(c(e18.mat), c(p65.mat)),</span>
<span class="line">                                             cor(c(p0.mat), c(p65.mat)),</span>
<span class="line">                                             cor(c(p4.mat), c(p65.mat)),</span>
<span class="line">                                             cor(c(p10.mat), c(p65.mat)),</span>
<span class="line">                                             cor(c(p18.mat), c(p65.mat)),</span>
<span class="line">                                             cor(c(p28.mat), c(p65.mat)), 1</span>
<span class="line">))</span>
<span class="line"># 绘制相关性随年龄变化的折线图</span>
<span class="line">ggplot(cor.to.p65, aes(x = age, y = cor)) + </span>
<span class="line">  geom_line(aes(linewidth = 4)) + </span>
<span class="line">  theme_classic() + </span>
<span class="line">  ylim(0.3, 1) + </span>
<span class="line">  theme(text = element_text(family = &quot;Myriad Pro&quot;)) + </span>
<span class="line">  theme(text = element_text(size = 20))   </span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></div></div></div></div><footer class="lp-footer-wrapper" vp-footer data-v-53848562 data-v-27bdb921><div class="lp-footer" data-v-27bdb921>Default footer<br data-v-27bdb921>Running time: 406 days 15 hours 23 minutes 26 seconds</div><div class="lp-copyright" data-v-27bdb921>Copyright © 2024 Miss Li </div></footer><!--]--></div><!--[--><!----><!--]--><!--]--></div>
    <script type="module" src="/assets/app-9eKqhTlB.js" defer></script>
  </body>
</html>
